<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class SectionController extends Controller
{
	//添加版块页面
    public function getAdd()
    {
    	//查询显示的分区
        $parts = DB::table('part')->where('status',2)->get();
        //解析模板
    	return view('admin.section.add',['parts'=>$parts]);
    }
    //添加版块操作
    public function postInsert(Request $request)
    {
        // dd($request->all());
        //添加操作
        $this->Validate($request, [
            'sectionname' => 'required|max:20',
        ],[
            'sectionname.required'=>'版块名不能为空',
            'sectionname.max'=>'版块名不能超过20个字符'
        ]);
        $arr = $request->only(['pid','sectionname','status']);
        $res = DB::table('section')->insert($arr);
        //判断添加是否成功
        if($res){
            return redirect('/admin/section/index')->with('success','版块名添加成功');
        }else{
            return back()->with('error','版块名添加失败');
        }
    }
    //分类列表
    public function getIndex(Request $request)
    {
        //获取 搜索
        $num = $request->input('num',10);
        if($request->input('keywords')){
            $sections = DB::table('section')->join('part','part.id','=','section.pid')->select('section.*','part.partname')->where('sectionname','like','%'.$request->input('keywords').'%')->paginate($num);   
        }else{
            $sections = DB::table('section')->join('part','part.id','=','section.pid')->select('section.*','part.partname')->paginate($num);
        }
        $data = $request->all();
        // dd($sections);
        //解析模板
        return view('admin.section.index',['sections'=>$sections,'request'=>$data]);
    }
    //修改
    public function getEdit($id)
    {
        //查询版块信息
        $section = DB::table('section')->where('id',$id)->first();
        // dd($section);
        //查询显示的分区
        $parts = DB::table('part')->where('status',2)->get();
        //查询该板块下是否有帖子
        $post = DB::table('post')->where('sid',$id)->get();
        if($post){
            //该版块下有帖子,不能修改
            return back()->with('error','该版块下有帖子不能修改');
        }else{
            //解析模板
            return view('admin.section.edit',['section'=> $section,'parts'=>$parts]);
        }
    }

    //执行修改
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        //修改操作
        $this->Validate($request, [
            'sectionname' => 'required|max:20',
        ],[
            'sectionname.required'=>'版块名不能为空',
            'sectionname.max'=>'版块名不能超过20个字符'
        ]);
        $id = $request->only('id');
        $arr = $request->only(['pid','sectionname','status']);
        $res = DB::table('section')->where('id',$id)->update($arr);
        //判断修改是否成功
        if($res){
            return redirect('/admin/section/index')->with('success','版块修改成功');
        }else{
            return back()->with('error','版块未做任何修改');
        }
    }

    //删除
    public function postDelete(Request $request)
    {
        //删除
        $id = $request->input('id');
        //查询该板块下是否有帖子
        $post = DB::table('post')->where('sid',$id)->first();
        if($post){
            //该版块下有帖子不能删除
            echo 3;
        }else{
            $res = DB::table('section')->where('id',$id)->delete();
            if($res){
                //删除成功
                echo 1;
            }else{
                //删除失败
                echo 2;
            }
        }
    }
}
